<?php
// 检查打卡积分的叶子菜单（三级菜单）
$host = '127.0.0.1';
$port = 3306;
$database = 'qiandao';
$username = 'root';
$password = 'root';

try {
    $pdo = new PDO("mysql:host={$host};port={$port};dbname={$database};charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    echo "=== 检查打卡积分的三级菜单（叶子节点） ===\n\n";
    
    // 查询打卡积分的所有子孙菜单
    $stmt = $pdo->query("
        SELECT m1.id as id1, m1.title as title1, m2.id as id2, m2.title as title2, m3.id as id3, m3.title as title3, m3.node, m3.url, m3.status
        FROM system_menu m1
        LEFT JOIN system_menu m2 ON m2.pid = m1.id
        LEFT JOIN system_menu m3 ON m3.pid = m2.id
        WHERE m1.id = 25
        ORDER BY m2.sort DESC, m2.id ASC, m3.sort DESC, m3.id ASC
    ");
    
    $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "打卡积分菜单结构:\n\n";
    
    $currentLevel2 = null;
    $leafCount = 0;
    $validLeafCount = 0;
    
    foreach ($rows as $row) {
        if ($row['id2'] && $row['title2'] != $currentLevel2) {
            $currentLevel2 = $row['title2'];
            echo "├── {$row['title2']} (ID:{$row['id2']})\n";
        }
        
        if ($row['id3']) {
            $leafCount++;
            $statusText = $row['status'] == 1 ? '启用' : '禁用';
            $hasNode = !empty($row['node']) ? '✓' : '✗';
            $hasUrl = !empty($row['url']) && $row['url'] != '#' ? '✓' : '✗';
            
            echo "│   └── {$row['title3']} (ID:{$row['id3']}, 状态:{$statusText}, Node:{$hasNode}, URL:{$hasUrl})\n";
            echo "│       Node: '{$row['node']}'\n";
            echo "│       URL: '{$row['url']}'\n";
            
            if ($row['status'] == 1 && !empty($row['node'])) {
                $validLeafCount++;
            }
        }
    }
    
    echo "\n=== 统计 ===\n\n";
    echo "三级菜单总数: {$leafCount}\n";
    echo "有效的三级菜单（启用且有node）: {$validLeafCount}\n";
    
    if ($validLeafCount == 0) {
        echo "\n⚠️ 警告: 没有有效的三级菜单！\n";
        echo "这可能是菜单不显示的原因。\n";
        echo "ThinkAdmin 可能只显示有可访问叶子节点的菜单。\n";
    }
    
    echo "\n=== 对比：系统管理的三级菜单 ===\n\n";
    
    $stmt = $pdo->query("
        SELECT m3.id, m3.title, m3.node, m3.url, m3.status
        FROM system_menu m1
        LEFT JOIN system_menu m2 ON m2.pid = m1.id
        LEFT JOIN system_menu m3 ON m3.pid = m2.id
        WHERE m1.id = 1 AND m3.id IS NOT NULL
        LIMIT 3
    ");
    
    $sysMenus = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    foreach ($sysMenus as $menu) {
        echo "- {$menu['title']}\n";
        echo "  Node: '{$menu['node']}'\n";
        echo "  URL: '{$menu['url']}'\n";
        echo "  Status: {$menu['status']}\n\n";
    }
    
} catch (PDOException $e) {
    echo "错误: " . $e->getMessage() . "\n";
}

